package com.sunday.shoufen.base;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;


import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.sunday.shoufen.R;
import com.sunday.shoufen.entity.Member;
import com.sunday.shoufen.service.LocationService;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

/**
 * Created by 刘涛 on 2017/10/16.
 */

public class BaseApp extends Application implements Thread.UncaughtExceptionHandler {
    private static BaseApp instance;

    public synchronized static BaseApp getInstance() {
        return instance;
    }


    private Member member;
    private String latitude;
    private String longitude;

    //public LocationService locationService;

    private Boolean isCanUpload = true;
    private boolean isLogin;
    private int payType;
    private String cityName;
    private int cityId;
    public LocationService locationService;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

//        if (getCurProcessName(this) != null && getCurProcessName(this).equals("com.sunday.shoufen")) {
//            /***
//             * 初始化定位sdk，建议在Application中创建
//             */
//            // UMShareAPI.get(getApplicationContext());
//            locationService = new LocationService(getApplicationContext());
//        Thread.setDefaultUncaughtExceptionHandler(this);
////            PlatformConfig.setWeixin(Constant.APP_ID, Constant.APPSecret);
////            Config.DEBUG = true;
////            Config.isJumptoAppStore = true;
//    }
        UMShareAPI.get(getApplicationContext());
        locationService = new LocationService(getApplicationContext());
        Thread.setDefaultUncaughtExceptionHandler(this);
        PlatformConfig.setWeixin(Constants.APP_ID, Constants.APPSecret);
        Config.DEBUG = true;
        Config.isJumptoAppStore = true;
    }

    public LocationService getLocationService() {
        return locationService;
    }

    private String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }


    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        e.printStackTrace();
        System.exit(0);
    }


    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean login) {
        isLogin = login;
    }

    public int getPayType() {
        return payType;
    }

    public void setPayType(int payType) {
        this.payType = payType;
    }


    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color._f5f4f9, R.color._666);//全局设置主题颜色
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
    }


    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public int getCityId() {
        return cityId;
    }

    public void setCityId(int cityId) {
        this.cityId = cityId;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }
}